package com.ming.web.queryperson.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ming.framework.mybatis.Page;
import com.ming.web.queryperson.service.PersonService;

@Controller
@RequestMapping("/queryPerson")
public class PersonController {
	
	private Logger log = LoggerFactory.getLogger(PersonController.class);
	@Autowired
	private PersonService personService;
	
	@RequestMapping(value = "")
	public String query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		request.setCharacterEncoding("UTF-8");
		
		String xm = request.getParameter("XM");
		
		return "pages/person/queryList";
	}
	
	@RequestMapping(value = "/queryListData", method = RequestMethod.POST)
	public void queryList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String openId = request.getParameter("openId");
		request.getSession().setAttribute("openId", openId);
		
		JSONObject data = JSONObject.fromObject(request.getParameter("data"));
		
		String pageNo = data.getString("pageNo");
		String pageSize = data.getString("pageSize");
		//total 前台grid总数的name，rows 前台grid数据集的name
		Page page = new Page("total", "rows", pageNo, pageSize);
		
		//查询条件集合
		Map<String, Object> paraMap = new HashMap<String, Object>();
		if (data.containsKey("XM")) {
			paraMap.put("XM", data.getString("XM"));
		}
		if (data.containsKey("JKZBH")) {
			paraMap.put("JKZBH", data.getString("JKZBH"));
		}
		if (data.containsKey("XB")) {
			paraMap.put("XB", data.getString("XB"));
		}
		List<Map> list = personService.getPagingData(page, paraMap);
		
		JSONObject json = new JSONObject();
		json.put("_dataCount", paraMap.get("_dataCount"));
		json.put("pageNo", page.getPageNum());
		json.put("pageSize", page.getPageSize());
		
		json.put("htmlData", getHtml(list, request.getContextPath()));
		log.info(json.toString());
		response.getWriter().write(json.toString());
	}
	
	private String getHtml(List<Map> list, String path) {
		StringBuffer html = new StringBuffer();;
		
		for(Map p : list) {
			html.append("<li class='ui-li-has-thumb ui-first-child'>");
			html.append("<a class='ui-btn ui-btn-icon-right ui-icon-carat-r' href='"
					+ path
					+ "/queryPerson/queryDetail?SERIAL_NO="
					+ p.get("SERIAL_NO") + "'>");
			html.append("<img src='" + path + "/imgs/common/nophoto.jpg' />");
			html.append("<h2>" + p.get("XM") + "</h2>");
			html.append("<p> 健康证卡号：" + p.get("JKZBH") + "</p></a></li>");
		}
		log.info(html.toString());
		return html.toString();
	}
	
	@RequestMapping(value = "/queryDetail", method = RequestMethod.GET)
	public String queryDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return "pages/person/detail";
	}
	@RequestMapping(value = "/getPersonInfo", method = RequestMethod.POST)
	public void getPersonInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sericalNo = request.getParameter("SERIAL_NO");
		
		Map<String, String> map = personService.getPersonInfo(sericalNo);
		
		JSONObject json = JSONObject.fromObject(map);
		
		response.getWriter().write(json.toString());
	}
	
}
